﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Server.model
{
	/// <summary>
	/// 类pay_type。
	/// </summary>
	[Serializable]
	public partial class pay_type
	{
		public pay_type()
		{}
		#region Model
		private int _id;
		private int _parent_id;
		private string _name;
		/// <summary>
		/// auto_increment
		/// </summary>
		public int id
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int parent_id
		{
			set{ _parent_id=value;}
			get{return _parent_id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string name
		{
			set{ _name=value;}
			get{return _name;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public pay_type(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,parent_id,name ");
			strSql.Append(" FROM [pay_type] ");
			strSql.Append(" where id=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["parent_id"]!=null && ds.Tables[0].Rows[0]["parent_id"].ToString()!="")
				{
					this.parent_id=int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null)
				{
					this.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperMySQL.GetMaxID("id", "pay_type"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [pay_type]");
			strSql.Append(" where id=@id ");

			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [pay_type] (");
			strSql.Append("parent_id,name)");
			strSql.Append(" values (");
			strSql.Append("@parent_id,@name)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@parent_id", MySqlDbType.Int32,11),
					new MySqlParameter("@name", MySqlDbType.VarChar,50)};
			parameters[0].Value = parent_id;
			parameters[1].Value = name;

			DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [pay_type] set ");
			strSql.Append("parent_id=@parent_id,");
			strSql.Append("name=@name");
			strSql.Append(" where id=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@parent_id", MySqlDbType.Int32,11),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
			parameters[0].Value = parent_id;
			parameters[1].Value = name;
			parameters[2].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [pay_type] ");
			strSql.Append(" where id=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 从name属性获取ID
		/// </summary>
		public void GetIDFromName(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id` ");
			strSql.Append(" FROM `pay_type` ");
			strSql.Append(" where `name`=@name ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar, 50)};
			parameters[0].Value = name;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
            }
		}

        /// <summary>
		/// 从ID属性获取name
		/// </summary>
		public void GetNameFromID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select `name`, `parent_id` ");
            strSql.Append(" FROM `pay_type` ");
            strSql.Append(" where `id`=@id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.VarChar, 50)};
            parameters[0].Value = id;

            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    this.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["parent_id"] != null && ds.Tables[0].Rows[0]["parent_id"].ToString() != "")
                {
                    this.parent_id = int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [pay_type] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

